#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <time.h>
#include "dominion.h"

/**
 * Sets up the initial gameState struct with all required variables
 * @param gameState - takes in a reference to a gameState which will be modified
 */
void initGame(struct gameState *game)
{
  int *k = malloc(sizeof(int) * 10);
  int seed = rand() % (INT_MAX - 1) + 1; // any random number above 1
  k = kingdomCards(0,1,2,3,4,5,6,7,8,9); // kindom cards required, if these values change, most of the tests fail
  initializeGame(2, k, seed, game); // must have an initialized game state

  return;
}

/**
 * displays whether the test failed or passed
 * @param  string   a char array (string) with the value being tested
 * @param  expected expected result of the test
 * @param  result   actual result of the test
 */
int check(char *string, int expected, int result, struct gameState *game)
{
  if (expected != result)
  {
    printf("FAIL - %s: expected - %d, result - %d\n", string, expected, result);
    return -1; // return an error
  }

  return 0; // successful
}

int testCase(struct gameState *game)
{
  // int drawCouncilRoomCard(int currentPlayer, struct gameState *state, int handPos)
  // adds 4 cards, adds 1 buy, each other player draws one card
  int handPos; // the position of the council card in the hand
  int numCardsBefore; // number of cards before adding
  int numCardsAfter; // number of cards after adding
  int numBuysBefore; // number of buys before council card
  int numBuysAfter; // number of buys after council card
  int currentPlayer; // 0 or 1 for player 
  for (int i = 0; i < 100; ++i)
  {
    currentPlayer = 0;//(rand() % 2); // randomly choose a player
    game->whoseTurn = currentPlayer;
    game->handCount[currentPlayer] == 0;
    for (int i = 0; i < 25; ++i) // draw the initial 5 cards
    {
      game->hand[currentPlayer][i] = rand() % 28;
    }
    game->handCount[currentPlayer] = 25;
    handPos = rand() % (game->handCount[currentPlayer] - 1); // the random location of the card
    game->hand[currentPlayer][handPos] = council_room; // set the location of the card to a random spot in the hand
    numCardsBefore = game->handCount[currentPlayer];
    numBuysBefore = game->numBuys;
    check("numCardsBefore", 5, numCardsBefore, game);
    check("numBuysBefore", 1, numBuysBefore, game);
    drawCouncilRoomCard(currentPlayer, game, handPos);
    numCardsAfter = game->handCount[currentPlayer];
    numBuysAfter = game->numBuys;
    check("numCardsAfter", 8, numCardsAfter, game);
    check("numBuysAfter", 2, numBuysAfter, game);
    /* reset important values */
    for (int i = 0; i < game->handCount[currentPlayer]; ++i)
    {
      discardCard(i, currentPlayer, game, 0); // remove all cards from the hand
    }
  }
}

int main(int argc, char const *argv[])
{
  struct gameState *game = malloc(sizeof(struct gameState));
  srand(time(NULL)); // seed the random number generator with the current time
  initGame(game);
  testCase(game);
  free(game); // always free your mem
  return 0;
}